package com.qf.service.impl;

import com.qf.dao.GoodsDao;
import com.qf.dao.GoodsTypeDao;
import com.qf.entity.Goods;
import com.qf.entity.GoodsType;
import com.qf.entity.PageBean;
import com.qf.service.GoodsService;
import com.qf.service.GoodsTypeService;
import com.qf.utils.MyBatisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author wgy
 * @version V1.0
 * @project gp12_xiaomi
 * @package com.qf.service.impl
 * @description: ${TODO}
 * @date 2022/6/23 11:39
 */
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {

    private GoodsDao goodsDao;

    private GoodsTypeService goodsTypeService;

    @Autowired
    public void setGoodsTypeService(GoodsTypeService goodsTypeService) {
        this.goodsTypeService = goodsTypeService;
    }

    @Autowired
    public void setGoodsDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }

    @Override
    public PageBean<Goods> findByPage(Map<Object, Object> map) {

        long totalSize=goodsDao.selectCount(map);
        List<Goods> data=goodsDao.selectByPage(map);
        PageBean<Goods> pageBean=new PageBean<>(Integer.parseInt((map.get("pn")).toString()),Integer.parseInt((map.get("ps")).toString()),totalSize, data);
        return pageBean;
    }

    @Override
    public Goods findById(int id) {

        Goods goods=goodsDao.selectById(id);
        if(goods==null){
            throw new RuntimeException("没有此商品");
        }
        //根据商品类别id查询

        GoodsType goodsType=goodsTypeService.findById(goods.getTypeid());
        goods.setGoodsType(goodsType);
        return goods;
    }

    @Override
    public void add(Goods goods) {

        goodsDao.insert(goods);


    }
}
